﻿/// <reference path="..\..\SignalR.Client.JS\Scripts\jquery-1.6.4.js" />
/// <reference path="jquery.signalR.js" />
(function ($, window, undefined) {
	/// <param name="$" type="jQuery" />
	"use strict";

	var signalR = $.signalR;

	function makeProxyCallback(hub, callback) {
		return function () {
			// Call the client hub method
			callback.apply(hub, $.makeArray(arguments));
		};
	}

	function registerHubProxies(instance, shouldSubscribe) {
		var key, hub, memberKey, memberValue, subscriptionMethod;

		for (key in instance) {
			if (instance.hasOwnProperty(key)) {
				hub = instance[key];

				if (!(hub.hubName)) {
					// Not a client hub
					continue;
				}

				if (shouldSubscribe) {
					// We want to subscribe to the hub events
					subscriptionMethod = hub.on;
				} else {
					// We want to unsubscribe from the hub events
					subscriptionMethod = hub.off;
				}

				// Loop through all members on the hub and find client hub functions to subscribe/unsubscribe
				for (memberKey in hub.client) {
					if (hub.client.hasOwnProperty(memberKey)) {
						memberValue = hub.client[memberKey];

						if (!$.isFunction(memberValue)) {
							// Not a client hub function
							continue;
						}

						subscriptionMethod.call(hub, memberKey, makeProxyCallback(hub, memberValue));
					}
				}
			}
		}
	}

	$.hubConnection.prototype.createHubProxies = function () {
		var proxies = {};
		this.starting(function () {
			// Register the hub proxies as subscribed
			// (instance, shouldSubscribe)
			registerHubProxies(proxies, true);

			this._registerSubscribedHubs();
		}).disconnected(function () {
			// Unsubscribe all hub proxies when we "disconnect".  This is to ensure that we do not re-add functional call backs.
			// (instance, shouldSubscribe)
			registerHubProxies(proxies, false);
		});

		proxies['defaultHub'] = this.createHubProxy('defaultHub');
		proxies['defaultHub'].client = {};
		proxies['defaultHub'].server = {
			ping: function (message) {
				return proxies['defaultHub'].invoke.apply(proxies['defaultHub'], $.merge(["Ping"], $.makeArray(arguments)));
			}
		};

		proxies['gameHub'] = this.createHubProxy('gameHub');
		proxies['gameHub'].client = {};
		proxies['gameHub'].server = {
			createRoom: function (roomId) {
				return proxies['gameHub'].invoke.apply(proxies['gameHub'], $.merge(["CreateRoom"], $.makeArray(arguments)));
			},

			draw: function (data) {
				return proxies['gameHub'].invoke.apply(proxies['gameHub'], $.merge(["Draw"], $.makeArray(arguments)));
			},

			findRoom: function () {
				return proxies['gameHub'].invoke.apply(proxies['gameHub'], $.merge(["FindRoom"], $.makeArray(arguments)));
			},

			getUsers: function () {
				return proxies['gameHub'].invoke.apply(proxies['gameHub'], $.merge(["GetUsers"], $.makeArray(arguments)));
			},

			getUsersCount: function () {
				return proxies['gameHub'].invoke.apply(proxies['gameHub'], $.merge(["GetUsersCount"], $.makeArray(arguments)));
			},

			getUsersList: function () {
				return proxies['gameHub'].invoke.apply(proxies['gameHub'], $.merge(["GetUsersList"], $.makeArray(arguments)));
			},

			initializeUser: function (name) {
				return proxies['gameHub'].invoke.apply(proxies['gameHub'], $.merge(["InitializeUser"], $.makeArray(arguments)));
			},

			ping: function (message) {
				return proxies['gameHub'].invoke.apply(proxies['gameHub'], $.merge(["Ping"], $.makeArray(arguments)));
			},

			sendMessage: function (msg) {
				return proxies['gameHub'].invoke.apply(proxies['gameHub'], $.merge(["SendMessage"], $.makeArray(arguments)));
			},

			startRound: function (wordId) {
				return proxies['gameHub'].invoke.apply(proxies['gameHub'], $.merge(["StartRound"], $.makeArray(arguments)));
			},

			userBan: function (userId) {
				return proxies['gameHub'].invoke.apply(proxies['gameHub'], $.merge(["UserBan"], $.makeArray(arguments)));
			}
		};

		return proxies;
	};

	signalR.hub = $.hubConnection("/signalr", { useDefaultPath: false });
	$.extend(signalR, signalR.hub.createHubProxies());

}(window.jQuery, window));